package cn.com.dycc.sys_manage.dictionarysysmanagement.service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.party_work.education.service.EduNotFoundException;
import cn.com.dycc.sys_manage.dictionarysysmanagement.dao.DictionaryDao;
import cn.com.dycc.sys_manage.dictionarysysmanagement.entity.Dictionary;
import cn.com.dycc.sys_manage.dictionarysysmanagement.entity.DictionaryResult;
import cn.com.dycc.sys_manage.dictionarysysmanagement.param.DicParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.Paging;


/**
 * 
 * Description: 
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2017年8月16日 下午4:05:42.
 * Update Date Time: 
 * @see
 */
@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService{
	@Resource
	private DictionaryDao dictionaryDao;
	
	@Resource(name="paging")
	private Paging paging;
	
	//增加字典表数据
	public String addDictionary(Dictionary dict) throws DictNotFoundException {
		String tab_name = dict.getTab_name();
		if(tab_name==null||tab_name.trim().isEmpty()){
			throw new DictNotFoundException("表名为空！");
		}
		String column_name = dict.getColumn_name();
		if(column_name==null||column_name.trim().isEmpty()){
			throw new DictNotFoundException("字段名不能为空！");
		}
		String key = dict.getKey();
		if(key==null||key.trim().isEmpty()){
			throw new DictNotFoundException("名称为空！");
		}
		String val = dict.getVal();
		if(val==null||val.trim().isEmpty()){
			throw new DictNotFoundException("值为空！");
		}
		Integer order_no = dict.getOrder_no();
		if(order_no==null){
			throw new DictNotFoundException("排序值不能为空！");
		}
		String state = dict.getState();
		if(state==null||state==""){
			dict.setState("Y");
		}
		
		String org_id = dict.getOrg_id();
		
		int count = 
				dictionaryDao.findcount(org_id, tab_name, column_name, key, val);
		
		if(count>=1){
			throw new DictNotFoundException("字典表已存在!");
		}
		
//		String format = "yyyy-MM-dd HH:mm:ss";
//		String update_date = DateUtil.getNowDate(format);
//		dict.setUpdate_date(update_date);
		
		int n = dictionaryDao.addDictionary(dict);
		if(n<=0){
			throw new DictNotFoundException("添加失败");
		}
		return "添加成功！";
	}
	
	//更新字典表数据
	public String updateDictionary(Dictionary dict) throws DictNotFoundException {
		String id = dict.getId();
		if(id==null){
			throw new DictNotFoundException("请选择需要更新的信息");
		}
		String key = dict.getKey();
		if(key==null||key.trim().isEmpty()){
			throw new DictNotFoundException("请填写名称！");
		}
		String val = dict.getVal();
		if(val==null||val.trim().isEmpty()){
			throw new DictNotFoundException("请填写值!");
		}
		String tab_name = dict.getTab_name();
		if(tab_name==null||tab_name.trim().isEmpty()){
			throw new DictNotFoundException("请填写表名！");
		}
		String column_name = dict.getColumn_name();
		if(column_name==null||column_name.trim().isEmpty()){
			throw new DictNotFoundException("请填写字段名！");
		}
		Integer order_no = dict.getOrder_no();
		String update_user_id =  dict.getUpdate_user_id();
		String org_id = dict.getOrg_id();
		String remake = dict.getRemake();
		String state = dict.getState();
		
		Dictionary lastDict = dictionaryDao.findDictionary(id,org_id);
		if(lastDict==null){
			throw new DictNotFoundException("该信息不存在");
		}
		Dictionary newDict = new Dictionary();
		if((!key.equals(lastDict.getKey()))||(!val.equals(lastDict.getVal()))||(!tab_name.equals(lastDict.getTab_name()))||(!column_name.equals(lastDict.getColumn_name()))){
//			System.out.println(key+val+tab_name+column_name);
			int count = 
					dictionaryDao.findcount(org_id, tab_name, column_name, key, val);
			System.out.println(count);
			if(count>=1){
				throw new DictNotFoundException("字典表已存在!");
			}
		}
		
		if(order_no!=null){
			newDict.setOrder_no(order_no);
		}
		newDict.setId(id);
		if(!key.equals(lastDict.getKey())){
			newDict.setKey(key);
		}
		if(!val.equals(lastDict.getVal())){
			newDict.setVal(val);
		}
		if(!tab_name.equals(lastDict.getTab_name())){
			newDict.setTab_name(tab_name);
		}
		
		newDict.setColumn_name(column_name);
		newDict.setOrg_id(org_id);
		newDict.setRemake(remake);
		newDict.setUpdate_user_id(update_user_id);
		newDict.setState(state);
		String format = "yyyy-MM-dd HH:mm:ss";
		String update_date = DateUtil.getNowDate(format);
		newDict.setUpdate_date(update_date);
		int n = dictionaryDao.updateDictionary(newDict);
		if(n<=0){
			throw new DictNotFoundException("修改失败");
		}
		return "更新成功！";
	}
	
	//批量删除字典表
	public String deleteDictionary(HttpServletRequest req,String[] ids) throws DictNotFoundException {
		if(ids==null||ids.length==0){
			throw new DictNotFoundException("请选择需要删除的字典表数据");
		}
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new DictNotFoundException("请重新登陆");
		}
		List<String> list = Arrays.asList(ids);
		System.out.println(list);
		int deleteNum = dictionaryDao.deleteDictionary(org_id,list);
		if(deleteNum!=ids.length){
			throw new DictNotFoundException("删除失败");
		}
		
		return "删除成功！";
	}
	
	//查询字典表所有信息 ;模糊查询
	public List<DictionaryResult> findAllDict(String org_id,DicParam dicParam) throws DictNotFoundException {
		String key = dicParam.getKey();
		String val = dicParam.getVal();
		String tab_name = dicParam.getTab_name();
		String column_name = dicParam.getColumn_name();
		/*Integer page = dicParam.getPage();
		Integer size = dicParam.getSize();
		Integer start = null;*/
		
		Integer page = dicParam.getPage();
		Integer limit = dicParam.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		//dicParam.setStart(start);
		
		List<DictionaryResult> list = dictionaryDao.findAllDict(org_id, key, val, tab_name,column_name, start, limit);
		return list;
	}
	
	//用于查询精确查询字典表集合
	public List<DictionaryResult> dicList(String org_id, DicParam dicParam) throws DictNotFoundException {
		String key = dicParam.getKey();
		String val = dicParam.getVal();
		String tab_name = dicParam.getTab_name();
		String column_name = dicParam.getColumn_name();
		/*Integer page = dicParam.getPage();
		Integer size = dicParam.getSize();
		Integer start = null;
		*/
		Integer page = dicParam.getPage();
		Integer limit = dicParam.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		//dicParam.setStart(start);
		List<DictionaryResult> list = dictionaryDao.dicList(org_id, key, val, tab_name,column_name, start, limit);
		return list;
	}

	public String countDict(String org_id, DicParam dicParam) throws DictNotFoundException {
		String key = dicParam.getKey();
		String val = dicParam.getVal();
		String tab_name = dicParam.getTab_name();
		String column_name = dicParam.getColumn_name();
		/*Integer page = dicParam.getPage();
		Integer size = dicParam.getSize();
		Integer start = null;*/
		
		Integer page = dicParam.getPage();
		Integer limit = dicParam.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		//dicParam.setStart(start);
		String str = dictionaryDao.countDict(org_id, key, val, tab_name, column_name);
		return str;
	}
	
	//查询字典表数据keyandval HttpServletRequest request, 
		public List<Map<String,Object>> findKeyAndVal(HttpServletRequest request,String tab_name, String column_name)
				throws DictNotFoundException {
			
			String org_id = request.getSession().getAttribute("org_id").toString();
			
//			String org_id = "1";
			
			if(tab_name==null||tab_name.trim().isEmpty()){
				throw new DictNotFoundException("请选择需要查询的表名！");
			}
			
			if(column_name==null||column_name.trim().isEmpty()){
				throw new DictNotFoundException("请选择需要查询的列名！");
			}
			
			Map<String,Object> map = new HashMap<String, Object>();
			
			List<Map<String,Object>> list = dictionaryDao.findKeyAndVal(org_id, tab_name, column_name);
			
//			if(list.size()==0){
//				map.put("key","");
//				map.put("val","");
//				list.add(map);
//			}
		
			return list;
		}

		public Dictionary findDictionary(String id, HttpServletRequest request) throws DictNotFoundException {
			if(id==null||id.trim().isEmpty()){
				throw new DictNotFoundException("ID为空");
			}
			HttpSession session = request.getSession();
			String user_id = (String) session.getAttribute("id");
			String org_id = session.getAttribute("org_id").toString();
			if(user_id==null){
				throw new DictNotFoundException("请重新登录！");
			}
			Dictionary dict = dictionaryDao.findDictionary(id,org_id);
			if(dict==null){
				throw new DictNotFoundException("查询失败！");
			}
			return dict;
		}


}
